#ifndef CONNECTION_H
#define CONNECTION_H

#include <QObject>
#include <QTcpSocket>

class OnetConnection : public QObject
{
    Q_OBJECT
public:
    // Creates disconnected onet connection.
    OnetConnection(QObject* parent);
    // Destroys onet connection. Disconnects if necessary.
    ~OnetConnection();

    // True if connection is working.
    bool IsConnected();
    // True if active - connecting, connected, sending or receiving.
    bool IsActive();

    bool OutgoingData();

public slots:
    // Starts connection to onet server.
    void Connect(QString aServerAddress, quint16 aServerPort);
    // Disconnects TCP.
    void Disconnect();
    // send data string to server
    void Send(QString aData);

protected slots:
    void onSocketError(QAbstractSocket::SocketError aError);
    void onConnected();
    void onDisconnected();
    void onStateChanged(QAbstractSocket::SocketState aState);
    void onReadyRead();
    void onReadChannelFinished();
    void onAboutToClose();
    void onBytesWritten(qint64 aBytes);

private:
    QTcpSocket* m_socket;

    enum State
    {
        NotConnected,
        Connecting,
        Idle,
        Sending,
        Receiving,
        Disconnecting,
    };

    State m_state;

    qint64 m_outgoingData;

    //QList<QByteArray> m_sendQueue;

signals:
    void Connected();
    void ConnectionError(int aError);
    void Disconnected();
    void DataSent(qint64 aBytes);
    void DataReceived(QString aData);
};

#endif // CONNECTION_H
